const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const ManifestPlugin = require('webpack-manifest-plugin');
const theme = require('./theme');
const CleanCSSPlugin = require('less-plugin-clean-css');

const extractCSS = new ExtractTextPlugin({
    filename: "[name]css.[contenthash].css",
    allChunks:true
});
const extractAntdLESS = new ExtractTextPlugin({
    filename: "[name]antdLess.[contenthash].css",
    allChunks:true
});
// const extractLESS = new ExtractTextPlugin({
//     filename: "[name]Less.[contenthash].css",
//     allChunks:false
// });

module.exports = {
    entry: {
        index:'./src/index.js',
        vendor: [
            'react'
        ]
    },
    output: {
        filename: "[name].[chunkhash].js",
        path: path.resolve(__dirname, 'devdist'),
        chunkFilename:"[id].[name].[chunkhash].js",
        publicPath: "/",
    },
    devtool: 'inline-source-map',
    module: {
        rules: [
            {
                test: /\.js$/,
                use: [
                    {
                        loader: 'cache-loader'
                    },
                    'babel-loader?cacheDirectory=true'
                ],
                include: path.resolve('src')
            },
            {
                test: /\.css$/,
                use: extractCSS.extract(
                   {
                   fallback: 'style-loader',
                   use:
                    [
                        // { loader: "style-loader/url" },
                        //{ loader: "file-loader" },
                        {
                            loader: 'css-loader',
                            options: { importLoaders: 2 }
                        },
                        {
                            loader:'resolve-url-loader'
                        },
                        'postcss-loader'
                    ]
                  }
                )
            },
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                test: /\.less$/,
                include: [
                    path.resolve(__dirname, "node_modules")
                ],
                use: extractAntdLESS.extract(
                   {
                   fallback: 'style-loader',
                   use:
                    [
                       // { loader: "style-loader/url" },
                       // { loader: "file-loader" },
                     //   { loader: "css-loader" },
                      //  'css-loader',
                        {
                            loader: 'postcss-loader'
                        },
                        {
                            loader: "less-loader",
                            options: {
                                modifyVars: theme
                            }
                        }
                    ]
                  }
                )
            },
            {
                test: /\.less$/,
                include: [
                     path.resolve(__dirname, "src")
                ],
                use:
                    [
                        { loader: "style-loader" },
                        {
                            loader: "css-loader",
                            options: {
                                modules: true,
                                localIdentName: '[name]_[local][hash:base64:5]',
                                importLoaders: 3,
                                sourceMap: true,
                                import:true
                            }
                        },
                        {
                            loader: 'postcss-loader',
                            options: {
                                //  modules: true,
                                //  localIdentName: '[name]_[local][hash:base64:5]',
                                //  importLoaders: 1,
                                sourceMap: true,
                                //   import:true,
                                //   strictMath: true,
                                //   noIeCompat: true
                            }
                        },
                        {
                            loader:'resolve-url-loader'
                        },

                        {
                            loader: "less-loader",
                            options: {
                                modules: true,
                                localIdentName: '[name]_[local][hash:base64:5]',
                              //  importLoaders: 1,
                                sourceMap: true,
                                import:true,
                                strictMath: true,
                                noIeCompat: true
                            }
                        }
                    ]
                 // use: extractLESS.extract(
                 //     {
                 //    fallback:[
                 //        { loader: "style-loader" }
                 //    ] ,
                 //    use:
                 //         [
                 //             { loader: "file-loader" },
                 //         {
                 //             loader: "extract-loader",
                 //             // options: {
                 //             //     publicPath: "../",
                 //             // }
                 //         },
                 //         {
                 //             loader: "css-loader",
                 //             options: {
                 //                modules: true,
                 //                 localIdentName: '[name]_[local][hash:base64:5]',
                 //                 importLoaders: 3,
                 //                 sourceMap: true,
                 //                 import:true
                 //             }
                 //         },
                 //         {
                 //             loader:'resolve-url-loader'
                 //         },
                 //         {
                 //             loader: "less-loader",
                 //             options: {
                 //                 modules: true,
                 //                 localIdentName: '[name]_[local][hash:base64:5]',
                 //                 importLoaders: 1,
                 //                 sourceMap: true,
                 //                 import:true,
                 //                 strictMath: true,
                 //                 noIeCompat: true
                 //             }
                 //         },
                 //         {
                 //             loader: 'postcss-loader'
                 //         }
                 //     ]
                 //  }
                 // )

            },
            {
                test: /\.(png|jpg|gif|eot|ttf|woff|woff2)$/,
                loader: 'url-loader',
                options: {
                    limit: 100000
                }
            },
            {
                test: /\.svg$/i,
                use: 'svg-sprite-loader',
                include: [
                    require.resolve('antd-mobile').replace(/warn\.js$/, ''), // 1. 属于 antd-mobile 内置 svg 文件
                    path.resolve(__dirname, 'src'), // 2. 自己私人的 svg 存放目录
                ]
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(['devdist']),
        new HtmlWebpackPlugin({
            title:"dva",
            template: 'src/index.html'
        }),
        extractCSS,
        extractAntdLESS,
      //  extractLESS,
        // new ExtractTextPlugin({
        //     filename: "[name].[contenthash].css"
        // }),
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: JSON.stringify("development"),
                BABEL_ENV:JSON.stringify("development")
            }
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            names: ["vendor", "common"],
            children: true,
            minChunks: 3,
            async: true
        }),
      //  new ManifestPlugin(),
    ],
    devServer: {
        contentBase: path.join(__dirname, "devdist"),
        compress: true,
        port: 3007,
        host: '0.0.0.0',
        //hot: true,
        //hotOnly: true,
        //noInfo: false,
        historyApiFallback: true,
        //解决版本升级问题
        disableHostCheck: false,
       // lazy: true,
        //filename: "[name].[chunkhash].js",
        //clientLogLevel: "none",
        //stats:"normal",
        overlay: {
            warnings: true,
            errors: true
        },
        proxy: {
            "/api/rest/api/v6.0": {
                //"target": "http://192.168.1.233/",
                "target": "http://172.17.20.88:8081",
                // "target": "http://testbbc.leimingtech.com",
                // "target": "http://116.196.114.131",
                // "target": "http://bbc.leimingtech.com",
                "changeOrigin": true,
                "logLevel": 'debug',
                "pathRewrite": { "^/api/rest/api/v6.0" : "/api/rest/api/v6.0" }
            }
        }
    }
};